package com.lumlord.service;

import com.lumlord.db.influx.InfluxDBConnection;
import com.lumlord.util.GsonTools;
import com.lumlord.webSocket.NettyWebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class LogDataService {

    public void log(Map<String,Object> log, String topic,boolean isAop){
        String serverName = (String) log.get("serverName");
        Map fields = (Map) log.get("fields");
        Map tags = (Map) log.get("tags");

        Object syncWebSocket = log.get("syncWebSocket");

        boolean syncToWebSocket = Boolean.valueOf(syncWebSocket==null?"false":syncWebSocket.toString());

        if(isAop){
            influxDBConnection.insert("logAction",tags,fields,0, TimeUnit.NANOSECONDS);
        }else{
            influxDBConnection.insert("log",tags,fields,0, TimeUnit.NANOSECONDS);
        }

        if(syncToWebSocket){
            Map webSocketJson = new HashMap();
            webSocketJson.putAll(fields);
            webSocketJson.putAll(tags);

            nettyWebSocketServer.sendUserProject("log",GsonTools.toJson(webSocketJson),true);
        }

    }



    @Autowired
    private InfluxDBConnection influxDBConnection;

    @Autowired
    private NettyWebSocketServer nettyWebSocketServer;
}
